package kr.co.creativeideas.gooq.view;

import kr.co.creativeideas.gooq.activity.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;

public class LineCircleView extends SurfaceView implements Callback {

	static boolean mRun = false;

	private Context mContext;
	private SurfaceHolder mSurfaceHolder;
	private LineCircleThread mThread;	
	private int sizeX, sizeY;
	private BitmapDrawable mBgImage;
	private Paint mPaint;
	private Paint sPaint;
	private Paint bPaint;
	private int mColor[] = { 0xFFE5195A, 0xFF5A34B2, 0xFFFFFF00, 0xFF195FE5, 0xFFFFFF00, 0xFFE65867, 0xFFD2B77E };
	private int sColor[] = { 0xFFFFFF00, 0xFF202123, 0xFF202123, 0xFFFFFFFF, 0xFF06BF22, 0xFF00FF00, 0xFFD5577E };
	private Canvas mCanvas;
	private float sx1, sy1, sx2, sy2;
	
	public LineCircleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		// TODO Auto-generated constructor stub
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		mSurfaceHolder = holder;
		mContext = context;
		
		mBgImage = (BitmapDrawable) getResources().getDrawable(R.drawable.tiled);
		mBgImage.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		mBgImage.setAlpha(0x11);
		setBackgroundDrawable(mBgImage);		 		
		init();
	}
	
	private void init() {
		Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		int width = display.getWidth();
		int height = display.getHeight();
		sizeX = width;
		sizeY = height;
		
		mCanvas = new Canvas();
		
		mPaint = new Paint();
		mPaint.setColor(0xFF32948A);
		mPaint.setStrokeWidth(10);
		mPaint.setAlpha(0xFF);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setAntiAlias(true);
		
		sPaint = new Paint();
		sPaint.setColor(0x00FF6F22);
		sPaint.setStrokeWidth(10);
		sPaint.setAlpha(0xFF);
		sPaint.setStyle(Paint.Style.STROKE);
		sPaint.setAntiAlias(true);
		
		bPaint = new Paint();
		bPaint.setColor(0x00000000);
		bPaint.setStrokeWidth(0);
		bPaint.setAlpha(0xFF);
		bPaint.setStyle(Paint.Style.FILL);
		bPaint.setAntiAlias(true);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		//mCanvas.save();
		//mCanvas.rotate(90);
		//canvas.drawLine(sizeX / 2, sizeY / 2, sx1, sy1, mPaint);		
		//canvas.restore();
		//canvas.drawLine(sizeX / 2, sizeY / 2, sx2, sy2, sPaint);		
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,	int height) {
		// TODO Auto-generated method stub	
		mSurfaceHolder = getHolder();		
		mThread = new LineCircleThread(mSurfaceHolder, mContext);
		mThread.setRunning(true);
		mThread.start();
		Log.i(kr.co.creativeideas.gooq.common.Constants.LOG_TAG,"change");	
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub				
		Log.i(kr.co.creativeideas.gooq.common.Constants.LOG_TAG,"create");		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		boolean retry = true;
		mThread.setRunning(false);		
		while (retry) {
			try {				
				mThread.join();
				Log.i(kr.co.creativeideas.gooq.common.Constants.LOG_TAG,"join");				
				retry = false;				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				doReset(holder);				
			}
		}
	}

	private void doDraw(Canvas canvas) {				
		canvas.save();	
		canvas.drawLine(sizeX / 2, sizeY / 2, sx1, sy1, mPaint);		
		canvas.restore();
		canvas.drawLine(sizeX / 2, sizeY / 2, sx2, sy2, sPaint);		
	}
	
	private void doReset(SurfaceHolder holder) {
			mCanvas = null;
			mCanvas = holder.lockCanvas();
			try {
				synchronized (holder) {
					mCanvas.save();
					mCanvas.drawRect(new Rect(0,0,mThread.width,mThread.height), bPaint);						
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {
				if (mCanvas != null) {						
					holder.unlockCanvasAndPost(mCanvas);
				}
			}			
	}

	class LineCircleThread extends Thread {
		
		SurfaceHolder mHolder;
		Context mContext;
		
		double angle = 0;
		int colorLength = mColor.length;
		int colorSelect = 0;
		int radius = 0;
		int width, height;

		public LineCircleThread(SurfaceHolder holder, Context context) {
			mHolder = holder;
			mContext = context;
			Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
			width = display.getWidth();
			height = display.getHeight();	
			radius();
		}
		
		public void radius() {
			angle = 270;
			int d = Math.min(width, height);
			radius = d - (d / 2);	
		}

		public void run() {
			//Log.i(kr.co.creativeideas.gooq.common.Constants.LOG_TAG,"On Run");			
			
			mCanvas = null;
			
			while (mRun) {
				//Log.i(kr.co.creativeideas.gooq.common.Constants.LOG_TAG,"On While Run");
				if (sy1 == sizeY / 2 +  (radius-30)) {
					mPaint.setColor(mColor[colorSelect]);
					sPaint.setColor(sColor[colorSelect]);
					colorSelect++;
					if (colorSelect == colorLength) {
						colorSelect = 0;
					}
				}								
				//mPoint.set(sx1, sy1);
				//sPoint.set(sx2, sy2);				
				mCanvas = mHolder.lockCanvas();
				
				sx1 = (float) (sizeX / 2 +  (radius-30) * (Math.cos(-(angle-180) * Math.PI / 180)));
				sy1 = (float) (sizeY / 2 +  (radius-30) * (Math.sin(-(angle-180) * Math.PI / 180)));
				
				sx2 = (float) (sizeX / 2 + (radius-15) * (Math.cos((angle) * Math.PI / 180)));
				sy2 = (float) (sizeY / 2 + (radius-15) * (Math.sin((angle) * Math.PI / 180)));
				
				angle += .2;
				
				try {
					synchronized (mHolder) {
						//onDraw(mCanvas);						
						//mCanvas.save();
						doDraw(mCanvas);						
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				} finally {
					if (mCanvas != null) {						
						mHolder.unlockCanvasAndPost(mCanvas);
					}
				}
			}
		}

		public void setRunning(boolean run) {
			mRun = run;
		}
	}	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();				
		switch (action) {
		case MotionEvent.ACTION_DOWN:			
			Log.i(kr.co.creativeideas.gooq.common.Constants.LOG_TAG, "Action_Down");
			surfaceDestroyed(mSurfaceHolder);			
			surfaceChanged(mSurfaceHolder, 0, mThread.width, mThread.height);
			doReset(mSurfaceHolder);
			//mSurfaceHolder = getHolder();
			//mThread = new LineCircleThread(mSurfaceHolder, mContext);
			//mThread.setRunning(true);
			//mThread.resume();
			//mThread.start();
			break;			
		case MotionEvent.ACTION_UP:
			Log.i(kr.co.creativeideas.gooq.common.Constants.LOG_TAG, "Action_Up");			
			surfaceDestroyed(mSurfaceHolder);
			doReset(mSurfaceHolder);
			//mThread.resume();
			break;
		}
		return true;
	}
}
